package com.aos.eap.activity;


import android.os.Handler;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.aos.eap.R;
import com.aos.eap.base.BaseDialogActivity;
import com.aos.eap.base.Constant;
import com.aos.eap.bean.EqpPmBean;
import com.aos.eap.bean.net.BaseResponseBean;
import com.aos.eap.utils.MyUtils;
import com.aos.eap.utils.NetUtils;
import com.google.gson.JsonObject;
import com.google.gson.reflect.TypeToken;
import com.lzy.okgo.model.Response;
import com.pda.platform.ui.ui_pdaplatform.callback.FreeUI_DialogEntityCallBack;
import com.pda.platform.ui.ui_pdaplatform.entity.FreeUI_AddViewEntity;
import com.pda.platform.ui.ui_pdaplatform.view.FreeUI_ClearEditText;

import butterknife.BindView;

public class MFG_WaitMaterialsActivity extends BaseDialogActivity {


    @BindView(R.id.tvUser)
    TextView tvUser;
    @BindView(R.id.etEqpId)
    FreeUI_ClearEditText etEqpId;
    @BindView(R.id.tvMainStatus)
    TextView tvMainStatus;
    @BindView(R.id.tvChildStatus)
    TextView tvChildStatus;
    @BindView(R.id.tvControlStatus)
    TextView tvControlStatus;
    @BindView(R.id.llMain)
    LinearLayout llMain;
    @BindView(R.id.etRemark)
    FreeUI_ClearEditText etRemark;

    private EqpPmBean eqpPmBean;

    @Override
    protected int getLayoutResId() {
        return R.layout.activity_all_change_eqp_status;
    }

    @Override
    protected void initView() {
        tvUser.setText(Constant.CURRENT_USER);
        MyUtils.setEditTextListener(etEqpId, this, this::requestInfoByEqpId);
    }

    private void requestInfoByEqpId(String eqpId) {
        JsonObject map = new JsonObject();
        map.addProperty("EqpID", eqpId);
        NetUtils.requestGetNet(this, "EMS_EqpStateLoad",  map, new FreeUI_DialogEntityCallBack<BaseResponseBean<EqpPmBean>>(
                new TypeToken<BaseResponseBean<EqpPmBean>>() {
                }.getType(), getSupportFragmentManager(), this) {
            @Override
            public void onSuccess(Response<BaseResponseBean<EqpPmBean>> response) {
                if (response.body().isSuccess(getApplicationContext(), true, getSupportFragmentManager())) {
                    eqpPmBean = response.body().getResponse().getBody();
                    setPageData(eqpPmBean);
                } else {
                    eqpPmBean = null;
                    setPageData(new EqpPmBean());
                }
            }

            @Override
            public void onError(Response<BaseResponseBean<EqpPmBean>> response) {
                super.onError(response);
                eqpPmBean = null;
                setPageData(new EqpPmBean());
                uploadLog(response.getException());
                loadError(response.getException(), "EMS_EqpStateLoad");
            }
        });
    }

    private void setPageData(EqpPmBean eqpPmBean) {
        tvMainStatus.setText(eqpPmBean.getMainMode());
        tvChildStatus.setText(eqpPmBean.getSubMode());
        tvControlStatus.setText(eqpPmBean.getCtrlMode());
        etRemark.setText(eqpPmBean.getRemark());
        if (eqpPmBean.getSubMode().equalsIgnoreCase("IDLE")){
            freeUI_titleView.setRightListener("开始", v -> {
                if (MyUtils.isFastClick()){
                    showToast("您点击的太快了",false);
                    return;
                }
                JsonObject map = new JsonObject();
                map.addProperty("EqpID", eqpPmBean.getEqpID());
                map.addProperty("MainMode", eqpPmBean.getMainMode());
                map.addProperty("SubMode", eqpPmBean.getSubMode());
                map.addProperty("CtrlMode", eqpPmBean.getCtrlMode());
                map.addProperty("TransCode", "Change_Material");
                map.addProperty("TransBtn", "start");
                map.addProperty("Remark", etRemark.getText().toString().trim());
                requestSubmit(map);
            });
        }else {
            freeUI_titleView.setRightListener("", v -> {
            });
        }

    }

    @Override
    protected void initData() {

    }

    @Override
    protected FreeUI_AddViewEntity getBaseEntity() {
        freeUI_titleView.setTitle("待料");
        freeUI_titleView.setLeftListener(view -> showBackDialog(MFG_WaitMaterialsActivity.this));
        return new FreeUI_AddViewEntity("header", llMain);
    }

    private void requestSubmit(JsonObject map) {
        NetUtils.requestGetNet(this, "EMS_EqpStateChange", map, new FreeUI_DialogEntityCallBack<BaseResponseBean<?>>(
                new TypeToken<BaseResponseBean<?>>() {
                }.getType(), getSupportFragmentManager(), this) {
            @Override
            public void onSuccess(Response<BaseResponseBean<?>> response) {
                if (response.body().isSuccess(getApplicationContext(), false, getSupportFragmentManager())) {
                    showToast("提交成功", true);
                    new Handler().postDelayed(MFG_WaitMaterialsActivity.this::finish, 2000);
                }
            }

            @Override
            public void onError(Response<BaseResponseBean<?>> response) {
                super.onError(response);
                uploadLog(response.getException());
                loadError(response.getException(), "EMS_EqpStateChange");
            }
        });
    }
}
